package cn.wht.deal;

import java.util.Map;
import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.wht.enginee.ParseEnginee;

/**
 * 处理解析后的文件
 * 
 * @Author: haoting.wang
 * @Date: Created in 上午11:21 2017/6/16
 */
public abstract class DealTask implements DealHandler, Runnable {

    protected BlockingQueue<Map> queue;
    private Logger               logger = LoggerFactory.getLogger(DealTask.class);
    private boolean              done   = false;

    public void setQueue(BlockingQueue<Map> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            while (!done) {
                Map data = (Map) queue.take();
                handle(data);
            }
        } catch (Exception e) {
            logger.error("import file into db error:" + e.getMessage(), e);
            try {
                Thread.interrupted();
            } catch (Exception ie) {
            }
            try {
                queue.put(ParseEnginee.DUMMY);
                setDone();
            } catch (Exception ex) {

            }
        }
    }

    protected void setDone() {
        done = true;
    }
}
